package net.test.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.ForeignKey;

import net.test.util.JsonUtil;
import net.test.util.StringUtil;

@Entity
public class Role extends BaseEntity{

	private static final long serialVersionUID = 1311016243976182049L;

	public static final String ROLE_BASE = "ROLE_INDEX";// 基础管理权限
	
	private String name;
	private String roleDesc;
	private String authority;
	
	private Set<Admin> adminSet=new HashSet<Admin>();

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getRoleDesc() {
		return roleDesc;
	}
	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	public String getAuthority() {
		return authority;
	}
	public void setAuthority(String authority) {
		this.authority = authority;
	}

	@ManyToMany(mappedBy = "roleSet", fetch = FetchType.LAZY)
	@ForeignKey(name = "role_admin_set")
	public Set<Admin> getAdminSet() {
		return adminSet;
	}
	public void setAdminSet(Set<Admin> adminSet) {
		this.adminSet = adminSet;
	}
	@Transient
	public List<String> getAuthorityList(){
		if(StringUtil.isEmpty(this.authority)){
			return null;
		}else{
			return JsonUtil.toObject(this.authority, ArrayList.class);
		}
		
	}
	@Transient
	public void setAuthorityList(List<String> roleList){
		if(roleList==null&&roleList.size()==0){
			this.authority=null;
			return ;
		}
		this.authority=JsonUtil.toJson(roleList);
	}
}
